package com.ygsoft.tianlu_mall.service.impl;

import com.ygsoft.tianlu_mall.mapper.CollectMapper;
import com.ygsoft.tianlu_mall.pojo.Collect2goods;
import com.ygsoft.tianlu_mall.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    CollectMapper collectMapper;

    @Override
    public boolean checkCollectedGoodsOnDB(int goods_id) {
        HashMap<String,Object> result = collectMapper.checkCollectedGoodsOnDB(goods_id);
        //如果非空，则允许收藏
        if (result!=null){
            return true;
        }else{
            return false;
        }

    }

    @Override
    @Transactional
    public boolean checkCollectedGoods(int user_id, int goods_id) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("goods_id",goods_id);
        HashMap<String,Object> collect2goods = collectMapper.checkCollectedGoods(map);
        //如果查询结果为空，则将要被收藏的商品不在数据库中，允许收藏，不允许删除
        if (collect2goods==null){
            return true;
        }else
            return false;
    }

    @Override
    @Transactional
    public void collectgoods(int user_id,int goods_id) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("goods_id",goods_id);
        collectMapper.collectgoods(map);
    }

    @Override
    @Transactional
    public void deleteOneGoods(int user_id, int goods_id) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("goods_id",goods_id);
        collectMapper.deleteOneGoods(map);
    }

    @Override
    @Transactional
    public List<Map<String,Object>> selectCollectGoods(int user_id) {
        return collectMapper.selectCollectGoods(user_id);
    }

    @Override
    public int getCollectGoodsCount(int user_id) {
        return collectMapper.getCollectGoodsCount(user_id);
    }

    @Override
    public boolean checkCollectedShopOnDB(int shop_id) {
        HashMap<String,Object> result = collectMapper.checkCollectedShopOnDB(shop_id);
        //如果非空，则允许收藏
        if (result!=null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    @Transactional
    public boolean checkCollectedShop(int user_id, int shop_id) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("shop_id",shop_id);
        HashMap<String,Object> collect2shop = collectMapper.checkCollectedShop(map);
        //如果查询结果为空，则将要被收藏的商铺不在数据库中，允许收藏，不允许删除
        if (collect2shop==null){
            return true;
        }else
            return false;
    }

    @Override
    @Transactional
    public void collectshop(int user_id, int shop_id) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("shop_id",shop_id);
        collectMapper.collectshop(map);
    }

    @Override
    @Transactional
    public void deleteOneShop(int user_id, int shop_id) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        map.put("shop_id",shop_id);
        collectMapper.deleteOneShop(map);
    }

    @Override
    public List<Map<String, Object>> selectCollectShop(int user_id) {
        return collectMapper.selectCollectShop(user_id);
    }

    @Override
    public List<Map<String, Object>> collectShopGood(int user_id) {
        return collectMapper.collectShopGood(user_id);
    }

    @Override
    public int getCollectShopCount(int user_id) {
        return collectMapper.getCollectShopCount(user_id);
    }

    @Override
    public List getGoodsIdByUserId(int user_id) {
        return collectMapper.getGoodsIdByUserId(user_id);
    }
}
